<template>
  <div class='container'>
    <el-form class="formBtn" ref="formDate" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="学科名称" prop="subjectName">
        <el-input class="input" v-model="form.subjectName" placeholder="请输入学科名称"></el-input>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch
          v-model="form.isFrontDisplay"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button @click="closeBtn">取 消</el-button>
        <el-button type="primary" @click="subBtn">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add, update } from '../../api/hmmm/subjects'
export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },

    editForm: {
      type: Object,
      required: true
    }

  },

  data () {
    return {
      form: {
        subjectName: '',
        isFrontDisplay: true
      },

      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },

  created () {
    if (this.isEdit) {
      this.form.id = this.editForm.id
      this.form.subjectName = this.editForm.subjectName
      if (this.editForm.isFrontDisplay === 1) {
        this.form.isFrontDisplay = true
      } else {
        this.form.isFrontDisplay = false
      }
    }
  },

  methods: {
    // 取消按钮
    closeBtn () {
      this.$emit('close')
    },

    // 点击确定按钮
    subBtn () {
      this.$refs.formDate.validate(async valid => {
        if (!valid) return
        // this.$emit('submit-add')
        if (!this.isEdit) {
          const res = await add(this.form)
          // console.log(res)
          if (res.status === 200 && res.statusText === 'OK') {
            this.$message.success('添加成功')
            this.$emit('submit-add')
          } else {
            return this.$message.success('添加失败')
          }
        } else {
          const res = await update(this.form)
          // console.log(res)
          if (res.status === 200 && res.statusText === 'OK') {
            this.$message.success('修改成功')
            this.$emit('submit-add')
          } else {
            return this.$message.success('修改失败')
          }
        }
        this.closeBtn()
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
